package com.ruoyi.web.controller.ZjgkjController;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ZipUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.domain.FileExportVo;
import com.ruoyi.web.domain.ZjgkjEntry;
import com.ruoyi.web.request.GetAllReq;
import com.ruoyi.web.service.MongoFileService;
import com.ruoyi.web.service.ZjgkjEntryService;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/ZjgkjEntry")
public class ZjgkjEntryController extends BaseController {

    @Resource
    private ZjgkjEntryService zjgkjEntryService;
    @Resource
    MongoFileService mongoFileService;

    @PostMapping("/getAll")
    public TableDataInfo getAll(@RequestBody GetAllReq req){
        startPage();
        List<ZjgkjEntry> zjgkjEntryList = zjgkjEntryService.getAll(req);
        return getDataTable(zjgkjEntryList);
    }

    @PostMapping("/addEntry")
    public AjaxResult addEntry(@RequestBody ZjgkjEntry zjgkjEntry){
        return zjgkjEntryService.addEntry(zjgkjEntry);
    }

    @PostMapping("/updateEntry")
    public AjaxResult updateEntry(@RequestBody ZjgkjEntry zjgkjEntry){
        return zjgkjEntryService.updateEntry(zjgkjEntry);
    }

    @GetMapping("/deleteEntry/{id}")
    public AjaxResult deleteEntry(@PathVariable("id") Long id){
        return zjgkjEntryService.deleteEntry(id);
    }

    @PreAuthorize("@ss.hasPermi('system:user:export')")
    @GetMapping("/exportEntry")
    public AjaxResult exportEntry(@RequestBody GetAllReq req)
    {
        List<ZjgkjEntry> zjgkjEntryList = zjgkjEntryService.getAll(req);
        ExcelUtil<ZjgkjEntry> util = new ExcelUtil<ZjgkjEntry>(ZjgkjEntry.class);
        return util.exportExcel(zjgkjEntryList, "用户填报数据");
    }

    @PreAuthorize("@ss.hasPermi('system:user:export')")
    @PostMapping("/exportFiles")
    public AjaxResult exportFiles(@RequestBody List<Long> fileIds, HttpServletResponse response){
        List<File> fileList = new ArrayList<>();
        for (Long fileId : fileIds) {
            FileExportVo fileExportVo = mongoFileService.downloadFile(fileId);
            try {
                // 创建临时文件
                File tempFile = File.createTempFile("temp"+fileId, ".txt");
                // 将byte数组写入临时文件
                FileOutputStream fos = new FileOutputStream(tempFile);
                fos.write(fileExportVo.getData());
                fos.close();
                // 使用临时文件创建File对象
                File file = new File(tempFile.getAbsolutePath());
                fileList.add(file);
                response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
                FileUtils.setAttachmentResponseHeader(response, "导出用户附件");
                ZipUtil.zip(response.getOutputStream(), Charset.defaultCharset(),
                        true, null, ArrayUtil.toArray(fileList, File.class));
            } catch (IOException e) {
                e.printStackTrace();
                return AjaxResult.error("导出失败:"+e.getMessage());
            }
        }
        return AjaxResult.success();
    }
}
